﻿using SqlSugar;
using NET6.WebAPI.Dtos.Tool;

namespace NET6.WebAPI.Extensions
{
    public static class SqlSugarExtension
    {
        /// <summary>
        /// 异步获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="pageNumber">页数</param>
        /// <param name="pageSize">页行数</param>
        /// <returns></returns>
        public static async Task<PagingDto<T>> GetPageListAsync<T>
            (this ISugarQueryable<T> queryable, int pageNumber, int pageSize) where T : class
        {
            RefAsync<int> totalCount = 0;
            RefAsync<int> totalPage = 0;

            var datas = await queryable.ToPageListAsync(pageNumber, pageSize, totalCount, totalPage);

            return new PagingDto<T>
            {
                TotalCount = totalCount,
                TotalPage = totalPage,
                Datas = datas
            };
        }
    }
}
